# -*- coding: utf-8 -*-
#爬取泰勒斯威夫特页面歌曲
import requests
import lxml.html
header={
        'User-Agent': 'Chrome/94.0.4606.61 Safari/537.36'}
url='https://music.163.com/artist?id=44266'
#网易云的外链地址
base_url='http://music.163.com/song/media/outer/url?id'
source=requests.get(url,headers=header)
sele=lxml.html.fromstring(source.text)
#r=source.text
#获取每首歌曲的id和名字
song_name=sele.xpath('//a[contains(@href,"song?")]/text()')
song_id=sele.xpath('//a[contains(@href,"song?")]/@href')
b=[]
for song_name1,song_id in zip(song_name,song_id):
    all=song_id.strip('/song?id=')
    if '$' not in all:
        song_url=base_url+'='+all
        bn=requests.get(song_url).content
        #处理歌名
        if song_name1=='...Ready For It? (BloodPop® Remix)':
            continue
        else:
           with open(song_name1+'.mp3','wb+') as f:
                f.write(bn)


            
    
